package com.tl.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
public class JDBCUtils {

	private static Logger log = Logger.getLogger(JDBCUtils.class);
	
	public static Connection getConn(){
		try{
			ResourceBundle b = ResourceBundle.getBundle("jdbc");
			String userName = b.getString("JDBC.userName");
			String password = b.getString("JDBC.password");
			String dirver = b.getString("JDBC.dirver");
			String url = b.getString("JDBC.url");
			
			Class.forName(dirver);
			return DriverManager.getConnection(url, userName, password);
		}catch(Exception e){
			e.printStackTrace();
			log.error("--------- 获取链接失败！------------");
		}
		return null;
	}
	
	/**
	 * 获取多列集合
	 * @param sql
	 * @return
	 */
	public static List<Hashtable<String,Object>> getQueryList(String sql){
		List<Hashtable<String,Object>> list = new ArrayList<Hashtable<String,Object>>(); 
		Connection conn = null;
		PreparedStatement pre = null;
		ResultSet rs = null;
		try{
			conn = getConn();
			pre = conn.prepareStatement(sql);
			rs = pre.executeQuery();
			ResultSetMetaData data = pre.getMetaData();
			
			while(rs.next()){
				Hashtable<String,Object> table = new Hashtable<String,Object>();
				for(int i=1;i<=data.getColumnCount();i++){
					table.put(data.getColumnName(i), rs.getObject(i));
				}
				list.add(table);
			}
		}catch(Exception e){
			log.error("----获取多列集合数据失败-----");
		}finally{
			closeConn(conn, pre, rs);
		}
		return list;
	}
	
	/**
	 * 获取单行集合
	 * @param sql
	 * @return
	 */
	public static Hashtable<String,Object> getQuerySingle(String sql){
		Hashtable<String,Object> table = new Hashtable<String,Object>();
		Connection conn = null;
		PreparedStatement pre = null;
		ResultSet rs  = null;
		try{
			conn = getConn();
			pre = conn.prepareStatement(sql);
			rs = pre.executeQuery();
			ResultSetMetaData data = pre.getMetaData();
			while(rs.next()){
				for(int i=1;i<=data.getColumnCount();i++){
					table.put(data.getColumnName(i), rs.getObject(i));
				}
			}
		}catch(Exception e){
			log.error("----获取单列集合数据失败-----");
		}finally{
			closeConn(conn, pre, rs);
		}
		return table;
	}
	
	/**
	 * 更新数据
	 * @param sql
	 */
	public static void updateData(String sql){
		Connection conn = getConn();
		PreparedStatement pre = null;
		try{
			conn = getConn();
			pre = conn.prepareStatement(sql);
			pre.executeUpdate();
		}catch(Exception e){
			log.error("----更新数据失败-----");
		}finally{
			closeConn(conn, pre, null);
		}
	}
	
	/**
	 * 删除数据
	 * @param sql
	 */
	public static void delData(String sql){
		Connection conn = getConn();
		PreparedStatement pre = null;
		try{
			conn = getConn();
			pre = conn.prepareStatement(sql);
			pre.execute();
		}catch(Exception e){
			log.error("----更新数据失败-----");
		}finally{
			closeConn(conn, pre, null);
		}
	}
	
	/**
	 * 释放连接
	 * @param conn
	 * @param prep
	 * @param res
	 */
	public static void closeConn(Connection conn,PreparedStatement prep,ResultSet res){
		try{
			if(res!=null){
				res.close();
				res = null;
			}
			if(prep!=null){
				prep.close();
				prep = null;
			}
			if(conn!=null){
				conn.close();
				conn = null;
			}
		}catch(Exception e){
			log.error("----关闭连接失败-----");
		}
	}
	
}
